package com.yzdmw.toutiao.user;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavDirections;
import androidx.navigation.Navigation;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yzdmw.toutiao.PersonalPageActivity;
import com.yzdmw.toutiao.R;
import com.yzdmw.toutiao.information_edit.edit_page;

import java.io.ByteArrayOutputStream;

public class UserSignedFragment extends Fragment {

    private UserSignedViewModel mViewModel;
    private TextView userName;
    private ImageView userImage;
    private Button homepage;

    public static UserSignedFragment newInstance() {
        return new UserSignedFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.user_signed_fragment, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(UserSignedViewModel.class);
        userName = this.getView().findViewById(R.id.user_userName);
        mViewModel.getUserName().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable final String name) {
                // Update the UI, in this case, a TextView.
                userName.setText(name);
            }
        });
        userImage = this.getView().findViewById(R.id.user_image);
        mViewModel.getUserImage().observe(getViewLifecycleOwner(), new Observer<Bitmap>() {
            @Override
            public void onChanged(@Nullable final Bitmap image) {
                // Update the UI, in this case, a TextView.
                if(image != null) {
                    userImage.setImageBitmap(image);
                }
            }
        });
        homepage = this.getView().findViewById(R.id.user_homepage);
        homepage.setOnClickListener(view -> {
            navigateToPersonalPage();
        });
    }

    private void navigateToPersonalPage() {
        Intent intent = new Intent(getContext(), PersonalPageActivity.class);
        new Thread() {
            @Override
            public void run() {
                intent.putExtra("name", mViewModel.getUserName().getValue());
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                Bitmap profile = mViewModel.getUserImage().getValue();
                if(profile != null) {
                    profile.compress(Bitmap.CompressFormat.JPEG,100,baos);
                    byte[] byteArray = baos.toByteArray();
                    intent.putExtra("profile",byteArray);
                }
                else {
                    intent.putExtra("profile","");
                }
                startActivityForResult(intent,14);
            }
        }.start();

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("USER","requestCode:"+requestCode+",resultCode:"+resultCode);
        if (requestCode == 14 && resultCode == 24) {
            String name = data.getStringExtra("name");
            byte[] byteArray = data.getByteArrayExtra("profile");
            if(name != null && !name.equals("")) {
                mViewModel.setUserName(name);
            }
            if(byteArray != null && byteArray.length != 0) {
                Bitmap profile = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
                mViewModel.setUserImage(profile);
            }
        }
    }
}